<template>
  <div class="summary-speech-container">
    <van-empty
      v-if="cp_conversationalSummary.length === 0"
      description="暂无内容"
    />
    <template v-else>
      <div class="top-info">
        <p>共{{ cp_conversationalSummary.length }}个发言人</p>
        <div class="copy-btn" @click="handleCopy">
          <img src="@/icons/svg/minute/copy_icon.svg" alt="copy" />
        </div>
      </div>
      <div
        class="summary-speech-cell"
        v-for="(item, index) in cp_conversationalSummary"
        :key="index"
      >
        <van-col align="center">
          <div class="summary-speech-title">{{ item.name || "-" }} :</div>
          <div class="review-btn" @click="onClickReview(item)">回顾</div>
        </van-col>
        <p>{{ item.Summary }}</p>
      </div>
    </template>
  </div>
</template>

<script>
import { mapState } from "pinia";
import { useMinutesStore } from "@/store";
import { showToast } from "vant";
import { addToClipboard } from "/src/utils/stringTool.js";

export default {
  name: "SummarySpeech",
  methods: {
    onClickReview(item) {
      console.log("click review: ", item);
      console.log("click speechOverviewData: ", this.speechOverviewData);

      
      const ui = item.ui;
      let speechOverviews = this.speechOverviewData.filter(v => v.uid === ui);
      let speechOverview = speechOverviews[0];
      console.log("click speechOverview: ", speechOverview);
      if (!speechOverview || speechOverview.asrList?.length === 0) {
        showToast("未匹配到内容");
        return;
      }
      let asrInfo = speechOverview.asrList[0];
      console.log("asrInfo: ", asrInfo);
      this.setPlayerTimeStamp(asrInfo.bt);
    },
    handleCopy() {
      const text = this.cp_conversationalSummary
        .map(item => `${item.name || "-"}:\n${item.Summary}`)
        .join("\n\n");

      addToClipboard(text);
      showToast("复制成功");
    }
  },
  computed: {
    ...mapState(useMinutesStore, {
      conversationalSummary: "getConversationalSummary",
      speechOverviewData: "speechOverviewData",
      asrList: "asrList",
      setPlayerTimeStamp: "setPlayerTimeStamp"
    }),

    cp_conversationalSummary() {
      let array = [];
      for (let index = 0; index < this.conversationalSummary.length; index++) {
        const element = this.conversationalSummary[index];
        let items = array.filter(item => item.name === element.name);
        if (items && items.length > 0) {
          let item = items[0];
          item.Summary += element.Summary;
        } else {
          array.push(element);
        }
      }
      return array;
    }
  }
};
</script>

<style lang="less" scoped>
.summary-speech-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 12px;
  box-sizing: border-box;
  padding: 0px 15px;

  .summary-speech-cell {
    // width: 80%;
    padding: 18px;
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 10px;
    background-color: #f9fafc;
    border-radius: 8px;
    gap: 6px;

    .summary-speech-title {
      width: 58px;
      font-weight: 400;
      font-size: 14px;
      color: #262626;
      line-height: 24px;
      white-space: normal; // 允许换行
      overflow: hidden; // 隐藏溢出部分
      text-overflow: ellipsis; // 溢出部分用省略号表示
    }

    p {
      font-weight: 400;
      font-size: 14px;
      color: #595959;
      line-height: 24px;
    }
  }
}

.review-btn {
  font-weight: 400;
  font-size: 12px;
  color: #436bff;
  line-height: 22px;
}

.top-info {
  width: 100%;
  font-weight: 400;
  font-size: 14px;
  color: #595959;
  line-height: 22px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;

  .copy-btn {
    font-size: 18px;
    color: #606266;
    background-color: white;
    border-radius: 4px;
    padding: 6px;
    cursor: pointer;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);

    img {
      width: 20px;
      height: 20px;
    }
  }
}
</style>
